package com.epoint.netty.future;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class PromiseFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
                promise.setSuccess(55);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 同步的方式获取数据（流程会卡在promise.get()这里）
        System.out.println("主线程额~");
        System.out.println(promise.getNow());
        System.out.println(promise.get());

        // 异步->注册事件的方式
    }
}
